package flexo.graphic.entity.particle;

import flexo.graphic.entity.ParticleSystem;
import flexo.math.Vector;

public abstract class AbstractEmitter implements ParticleAffector
{
	public abstract Vector getNewPosition(ParticleSystem sys);
	
	public void restart(Particle p)
	{
		float newLife = (1 - p.system.getRandom()) * p.system.getMaxLife();
		float newSize = p.system.getDefaultSize() * (p.system.getRandom() + 0.5f);
		p.reset(newLife, newSize);
		p.color.set(p.system.getColor());
	}

	@Override
	public void affect(Particle particle)
	{
		if (particle.isDead())
		{			
			restart(particle);
						
			Vector v = getNewPosition(particle.system);			
						
			particle.origin.set(v);
			
			if (!particle.system.isStatic())
			{
				Vector abs = particle.system.getWorldNode().getAbsolutePosition();
				particle.origin.x += abs.x;
				particle.origin.y += abs.y;
				particle.origin.z += abs.z;
			}
			
			particle.position.set(particle.origin);
		}
	}
}
